﻿


namespace KatakPresentationLayer.Web
{
    using System.Linq;
    using System.Security.Principal;
    using System.Web.DomainServices;
    using System.Web.Ria;
    using System.Web.Ria.ApplicationServices;

    [EnableClientAccess]
    public class AuthenticationService : AuthenticationBase<User>
    {
        private static User DefaultUser = User.FromUserData(
            UserData.CreateUserData(0, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty));

        /*
         * Quick Start:
         *  This domain service is designed to use composition. This allows any number of
         *  implementations to be supported following roughly the same pattern.
         */
        private KataService _service = new KataService();

        /*
         * Quick Start:
         *  User validation is possible to implement by overriding the ValidateUser method.
         *  However, it is typically preferrable to write a full MembershipProvider for 
         *  reasons of security and reuse.
         */
        protected override bool ValidateUser(string username, string password)
        {
            return _service.GetUserData().Any(u => u.Username == username && u.Password == password);
        }

        protected override User GetAuthenticatedUser(IPrincipal principal)
        {
            UserData userData =
                this._service.GetUserData().First(u => u.Username == principal.Identity.Name);

            User user = User.FromUserData(userData);
            user.AuthenticationType = principal.Identity.AuthenticationType;
            //user.IsAuthenticated = principal.Identity.IsAuthenticated;

            return user;
        }

        protected override User GetAnonymousUser()
        {
            return DefaultUser;
        }

        //protected override void UpdateUserCore(User user, User original)
        //{
        //    UserData userData = User.ToUserData(user);
        //    UserData originalUserData = User.ToUserData(original);

        //    // Essentially we want to write:
        //    //
        //    // this._service.UpdateUser(userData, originalUserData);
        //    //
        //    // However, here's how we write it such that it gets executed correctly on the 
        //    // current data stack:
        //    EntityOperation operation = new EntityOperation();
        //    operation.Entity = userData;
        //    operation.OriginalEntity = originalUserData;
        //    operation.Operation = DomainOperation.Update;

        //    //this._service.Submit(new ChangeSet(new List<UserData> { operation }));
        //}

        // Methods overridden to support composition
        public override void Initialize(DomainServiceContext context)
        {
            this._service.Initialize(context);
            base.Initialize(context);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this._service.Dispose();
            }
            base.Dispose(disposing);
        }
    }



    public class User : UserBase
    {
        public string FullName { get; set; }
        public string PhoneNumber { get; set; }

        internal static User FromUserData(UserData userData)
        {
            return new User()
            {
                Name = userData.Username,
                Roles = userData.Roles.Split(','),
                FullName = userData.FullName,
                PhoneNumber = userData.PhoneNumber,
            };
        }

        internal static UserData ToUserData(User user)
        {
            return UserData.CreateUserData(0, user.FullName, "", user.Roles.ToString(), "","");
        }
    }

}




